package model

import (
	"gorm.io/gorm"
	"video-server/basic/config"
)

type Video struct {
	gorm.Model
	Title string `gorm:"type:varchar(30)" json:"title"`
	Cover string `gorm:"type:varchar(100)" json:"cover"`
	Type  string `gorm:"type:varchar(30)" json:"type"`
}

func (v *Video) CreateVideo() error {
	return config.DB.Create(&v).Error
}

type Drama struct {
	gorm.Model
	VideoId      int    `gorm:"type:int" json:"video_id"`
	EpisodeId    int    `gorm:"type:int" json:"episode_id"`
	Name         string `gorm:"type:varchar(30)" json:"name"`
	Type         string `gorm:"type:varchar(30)" json:"type"`
	Introduction string `gorm:"type:varchar(50)" json:"introduction"`
}

func (d *Drama) CreateDrama() error {
	return config.DB.Create(&d).Error
}

func (d *Drama) CreateAnimals(db *gorm.DB) error {
	// 再唠叨一下，事务一旦开始，你就应该使用 tx 处理数据
	tx := db.Begin()
	defer func() {
		if r := recover(); r != nil {
			tx.Rollback()
		}
	}()

	if err := tx.Error; err != nil {
		return err
	}

	if err := tx.Create(&d).Error; err != nil {
		tx.Rollback()
		return err
	}

	return tx.Commit().Error
}

type AuditRecord struct {
	gorm.Model
	VideoId   int    `gorm:"type:int" json:"video_id"`
	AuditorId int    `gorm:"type:int" json:"auditor_id"`
	Status    int    `gorm:"type:tinyint;default:0;not null;comment:'状态 0-待审核 1-审核中 2-通过 3-驳回'"`
	Opinion   string `gorm:"type:varchar(20)" json:"opinion"`
}

func (a *AuditRecord) CreateAuditRecord() error {
	return config.DB.Create(&a).Error
}

type Permission struct {
	gorm.Model
	UserId  int    `gorm:"type:int" json:"user_id"`
	VlpTime string `gorm:"type:varchar(20)" json:"VlpTime"`
}

func (p *Permission) CreatePermission() error {
	return config.DB.Create(&p).Error
}
